package com.loyal.loyalerp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.loyal.loyalerp.basedata.company.mapper.GyCompanyMapper;
import com.loyal.loyalerp.mapper.GyUserRoleMapper;
import com.loyal.loyalerp.model.GyUserRole;
import com.loyal.loyalerp.service.UserRoleService;

@RestController
public class UserRoleController {
	
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private GyUserRoleMapper userRoleMapper;
	@Autowired
	private GyCompanyMapper companyMapper;
	
	@GetMapping("/listAll")
	public List<GyUserRole> listAll(){
		List<GyUserRole> list = new ArrayList<GyUserRole>();
		list = userRoleService.listAll();
		if(list == null) {
			return null;
		}
		return list;
	}
	
	@PostMapping("/addUserRole")
	public Map<String,Object> addUserRole(HttpServletRequest request){
		int status = 0;
		String msg = null;
		Map<String, Object> statusMap = new HashMap<String,Object>();
		Map<String, Integer> map = new HashMap<String,Integer>();
		String tradename = request.getParameter("companyname");
		String rolename = request.getParameter("rolename");
		String usercode = request.getParameter("usercode");
		map = userRoleService.getID(usercode, rolename, tradename);
		int userId = map.get("userId");
		int roleId = map.get("roleId");
		int companyId = map.get("companyId");
		GyUserRole userrole = new GyUserRole(companyId,roleId,userId);
		try {
			userRoleService.insertUserRole(userrole);
			status = 200;
			statusMap.put("status", status);
			return statusMap;
		}catch(Exception e) {
			status = 500;
			msg = "已经存在此角色！";
			statusMap.put("status", status);
			statusMap.put("msg", msg);
			return statusMap;
		}
	}
	
	@PostMapping("/delUserRole")
	public void delUserRole(HttpServletRequest request){
		String rolecode = request.getParameter("rolecode");
		String usercode = request.getParameter("usercode");
		String companyname = request.getParameter("companyname");
		try {
			int companyid = companyMapper.selectCompanyIDByName(companyname);
			userRoleMapper.deleteByCode(Integer.parseInt(usercode), Integer.parseInt(rolecode),companyid);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@PostMapping("/editUserRole")
	public Map<String,Object> updateUserRole(HttpServletRequest request) {
		Map<String,Object> hashMap = new HashMap<String,Object>();
		String msg = null;
		String status = "200";
		String id = request.getParameter("id");
		String rolecode = request.getParameter("rolecode");
		String rolename = request.getParameter("rolename");
		if(!userRoleService.EditRole(Integer.parseInt(id)) ){
			msg = "发生未知异常";
			status = "500";
			hashMap.put("msg", msg);
			hashMap.put("status", status);
		}
		hashMap.put("msg", msg);
		hashMap.put("status", status);
		return hashMap;
	}
}
